<!DOCTYPE html>
<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console,model" />
<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min"></uieip>
    <uieip type="tmpl" value="model.title"></uieip>
</head>

<body style="margin: 0;">
    <table id="tb_cms">
    </table>
    <script>
        <uieip type="tmpl" value="model.params"></uieip>

        $(function () {
            main_fun.init();
        });

        var page_fun = {
            init: function () {
                $(window).resize(function () {
                    page_fun.onresize();
                });
                page_fun.onresize();
                if (main_fun.options().loadData) {
                    main_fun.load_data();
                }
            },
            onresize: function () {
                main_fun.resize({
                    width: $(window).width(),
                    height: $(window).height()
                });
            },
        }

        var main_fun = {
            id: "tb_cms",
            init: function () {
                var _loadData = _tb.loadData;
                var opt = $.extend(_tb, {
                    loadData: false,
                    menuType: 3,
                    afterBindingTitle: function () {
                        this.yqDataGrid("options").loadData = _loadData;
                        this.yqDataGrid("btnDisable", "download");
                        this.yqDataGrid("btnDisable", "viewlog");
                        page_fun.init();
                    },
                    onBeforeShowEdit: function (titles, action, row, callback) {      //add/upp/move 编辑执行前
                        //console.log("onBeforeShowEdit", action, row);
                        if (action == "add") {
                            row["time"] = new Date();
                        }
                        callback(true, titles, row);      //true 执行编辑
                    },
                    editLinks: [{}, {
                        id: "download",
                        text: "下载证书",
                        iconCls: "fa fa-download",
                    }, {
                        id: "viewlog",
                        text: "查看日志",
                        iconCls: "fa fa-list",
                    }],
                    onClickRow: function (index, row) {     //点击行
                        this.yqDataGrid((row === null ? "btnDisable" : "btnEnable"), "download");
                        this.yqDataGrid((row === null ? "btnDisable" : "btnEnable"), "viewlog");
                    },
                    btnAction: function (action, params, callback) {
                        if (action == "download") {
                            var row = $("#tb_cms").yqDataGrid("getSelected");
                            if (row === null) {
                                console.log("请选择要下载的证书");
                                callback(false);
                                return;
                            }

                            var url = "/api/license/local.json?sid=" + row["sid"];
                            var filename = "license.json";

                            // 创建下载链接并触发下载
                            $.downloadFile(url, filename);


                            console.log("download", row);
                            callback(false);
                        } else if (action == "viewlog") {
                            var row = $("#tb_cms").yqDataGrid("getSelected");
                            if (row === null) {
                                console.log("请选择要查看日志的证书");
                                callback(false);
                                return;
                            }

                            log_fun.open(row["sid"]);
                            callback(false);
                        } else {
                            callback(true);
                        }
                    },
                });

                $("#" + main_fun.id).yqDataGrid(opt);
            },
            load_data: function () {
                $("#" + main_fun.id).yqDataGrid("loadData");
            },
            options: function () {
                return $("#" + main_fun.id).yqDataGrid('options');
            },
            resize: function (para) {
                $("#" + main_fun.id).yqDataGrid('resize', para);
            }
        }

        var log_fun = {
            sid: null,
            $body: null,
            open: function (sid) {
                var $this = this;
                this.sid = sid;

                var w = $(window).width();
                var h = $(window).height();
                if (w > 1000) {
                    w = 1000;
                }
                if (h > 800) {
                    h = 800;
                }

                var pop_opt = {
                    prevent_e: {      //调用组件信息
                        clientX: 0,
                        clientY: 0
                    },
                    mdiinfo: {            //mdi窗体信息
                        clientX: 0,
                        clientY: 0
                    },
                    showTitle: true,
                    title: "查看日志",
                    width: w,
                    height: h,
                    horizontalAlign: "center",
                    verticalAlign: "center",
                    buttons: [],
                    closeBtn: false,
                    iframe: false,
                    iframeUrl: "",
                    iconCls: "fa fa-list",
                    onOpen: function ($body) {
                        $this.$body = $body;
                        $this.init();
                    },
                    onResize: function (width, height) {
                        //console.log("onResize", width, height);
                    },
                    onBeforeClose: function () {
                        return true;
                    },
                    onClose: function () {
                    }
                };

                top.$.yqDialog(pop_opt);
            },

            init: function () {
                var opt = $.extend(_log, {
                    bindingValue: this.sid,
                    afterBindingTitle: function () {

                    },
                    onBeforeShowEdit: function (titles, action, row, callback) {      //add/upp/move 编辑执行前
                        callback(true, titles, row);      //true 执行编辑
                    },
                });

                this.$body.yqDataGrid(opt);
            },
        }
    </script>
</body>

</html>